package com.yihuier.yihuier.Service.MeetingManager;

import com.yihuier.yihuier.Entity.MeetingManager.DTO.RoomTimeDTO;
import com.yihuier.yihuier.Entity.RoomManager.Room;
import com.yihuier.yihuier.Mapper.MeetingManager.MeetingRoomMapper;
import com.yihuier.yihuier.Mapper.RoomManager.RoomMapper;
import com.yihuier.yihuier.Mapper.UserManager.DepartmentMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

@Service
public class MeetingRoomService {

    @Autowired
    private MeetingRoomMapper meetingRoomMapper;

    @Autowired
    private DepartmentMapper departmentMapper;

    @Autowired
    private RoomMapper roomMapper;

    public int [] getMeetingsByTimeAndRoom(Date before,Date after,int room_id){
        return meetingRoomMapper.selectMeetingsByTimeAndRoom(before,after,room_id);
    }

    public int [] getMeetingsByTime(Date before,Date after){
        return meetingRoomMapper.selectMeetingsByTime(before,after);
    }

    public int getNumMeetingsByRoom(Date before,Date after,int room_id){
        return meetingRoomMapper.selectNumMeetingsByRoom(before,after,room_id);
    }

    public int[] getMeetingIds(int[] room_ids){
        return meetingRoomMapper.selectMeetingIds(room_ids);
    }

    public Map<String,Object> getMeetingTimes(Long room_id,Long meeting_id){
        return meetingRoomMapper.selectMeetingTimes(room_id,meeting_id);
    }

    public int getNumMeetings(Date before,Date after){
        return meetingRoomMapper.selectNumMeetings(before,after);
    }

    public List<Map<String,Object>> getTopThreeDepartmentRatio(Date before,Date after,int room_id){
        List<Map<String,Object>> result = meetingRoomMapper.selectTopDepartmentByTimeAndRoom(before,after,room_id);
        for (Map<String,Object> nodeData : result){
            nodeData.put("department_name",departmentMapper.selectNameById((int)((long)nodeData.get("department_id"))));
        }
        return result;
    }

    public Room getFitRoom(int user_id,Date startTime, int duringTime, int numOfPeople,List<String> gears){
        int [] rooms = this.roomMapper.selectAllRoomIdsCanUse(user_id,numOfPeople);
        if (rooms == null || rooms.length == 0){
            return null;
        }
        boolean[] gearFit = new boolean[rooms.length];
        int tryToFitGearsSize = gears.size();
        for (int i = 0 ; i < gearFit.length ; i ++){
            gearFit[i] = (tryToFitGearsSize == 0);
        }
        for (int i = 0 ; i < rooms.length ; i++){
            String tempGear = this.roomMapper.selectRoomsGear(rooms[i]);
            if (tempGear == null){

            }
            String [] gear = tempGear.split(",");

        }
        return new Room();
    }

    public Room canThisTimeBeOrderd(){
        return new Room();
    }


    public void checkAvailble(List<RoomTimeDTO> roomTimeDTOS) throws ParseException {
        Calendar calendar = new GregorianCalendar();
        Date start;
        Date end;

        for (RoomTimeDTO temp : roomTimeDTOS) {
            Map<String,Boolean> timeCheck = temp.getTimeCheck();
            calendar.setTime(new Date(temp.getStartDay().getTime()));
            calendar.set(Calendar.HOUR_OF_DAY,8);
            calendar.set(Calendar.MINUTE,0);
            calendar.set(Calendar.SECOND,0);
            // 8-10
            start = calendar.getTime();
            calendar.set(Calendar.HOUR_OF_DAY,10);
            end = calendar.getTime();
            if (this.meetingRoomMapper.selectThisTimeRoomIfUsing(temp.getRoom_id(),start,end) > 0){
                timeCheck.put("8:00",false);
            }else {
                timeCheck.put("8:00",true);
            }

            // 10-12
            start = calendar.getTime();
            calendar.set(Calendar.HOUR_OF_DAY,12);
            end = calendar.getTime();
            if (this.meetingRoomMapper.selectThisTimeRoomIfUsing(temp.getRoom_id(),start,end) > 0){
                timeCheck.put("10:00",false);
            }else {
                timeCheck.put("10:00",true);
            }

            // 14-16
            calendar.set(Calendar.HOUR_OF_DAY,14);
            start = calendar.getTime();
            calendar.set(Calendar.HOUR_OF_DAY,16);
            end = calendar.getTime();
            if (this.meetingRoomMapper.selectThisTimeRoomIfUsing(temp.getRoom_id(),start,end) > 0){
                timeCheck.put("14:00",false);
            }else {
                timeCheck.put("14:00",true);
            }

            // 16-18
            start = calendar.getTime();
            calendar.set(Calendar.HOUR_OF_DAY,18);
            end = calendar.getTime();
            if (this.meetingRoomMapper.selectThisTimeRoomIfUsing(temp.getRoom_id(),start,end) > 0){
                timeCheck.put("16:00",false);
            }else {
                timeCheck.put("16:00",true);
            }
        }
    }
}
